package example

import (
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

type ForcastOne struct {
	gorm.Model
	Label      string
	Samples    int
	Average    int
	Rate_50    int
	Rate_90    int
	Rate_95    int
	Rate_99    int
	Min        int
	Max        int
	Error      float32
	Throughput float32
	Received   float32
	Sent       float32
}

var Db *gorm.DB

func init() {
	//"root:cloudera_mysql_123@tcp(10.16.48.77:3306)/test")
	db, err := gorm.Open("mysql", "root:cloudera_mysql_123@tcp(10.16.48.77)/test?charset=utf8&parseTime=True&loc=Local")
	if err != nil {
		panic("failed to connect database")
	}
	Db = db
	//defer db.Close()
}

func Exapmle02() {

	// Migrate the schema
	Db.AutoMigrate(&ForcastOne{})
	// HTTP请求,386,7628,7295,12809,15097,17305,1659,17862,0.000%,21.16112,28687.39,3.62
	// Create
	//Db.Create(&ForcastOne{Label: "HTTP请求", Samples: 386, Average: 7628, Rate_50: 7295, Rate_90: 12809, Rate_95: 15097, Rate_99: 17305, Min: 1659, Max: 17862, Error: 0, Throughput: 21.16112, Received: 28687.39, Sent: 3.62})

	// Read
	var forcastOne ForcastOne
	Db.First(&forcastOne, 3) // find product with id 1
	//Db.First(&forcastOne, "Samples = ?", "386") // find product with code l1212

	// Update - update product's price to 2000
	//Db.Model(&forcastOne).Update("Average", 7628)

	// Delete - delete product
	//Db.Delete(&forcastOne)
	Db.Unscoped().Delete(&forcastOne)
}
